import socket

def main():
    # 服务器地址和端口
    server_ip = '127.0.0.1'  # 如果是远程服务器，请改成对应IP
    server_port = 8000

    # 创建 socket 并连接
    client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    client_socket.connect((server_ip, server_port))

    print(f"Connected to server at {server_ip}:{server_port}")
    print("Type messages to send. Type 'exit' to quit.\n")

    try:
        while True:
            msg = input("You: ")
            if msg.lower() == 'exit':
                break
            client_socket.sendall(msg.encode())
    except KeyboardInterrupt:
        print("\nDisconnected by user.")
    finally:
        client_socket.close()
        print("Connection closed.")

if __name__ == "__main__":
    main()
